/**
 * 表格渲染
 * @param {*} objData.elem 需要渲染表格的父容器
 * @param {*} objData.model 表格的模块
 * @param {*} objData.pk 表格的主键
 * @param {*} objData.cols 表格的字段
 * @param {*} objData.tableApi 表格数据接口地址
 * @param {*} objData.updateApi 表格记录更新接口地址
 * @param {*} objData.delApi 表格记录删除接口地址
 */
function renderTable(objData) {
    if (objData.elem == null) return false;
    if (objData.model == null) return false;
    var tableData = {
        elem: objData.elem,
        model: objData.model,
        pk: objData.pk,
        cols: objData.cols,
        tableApi: objData.tableApi,
        updateApi: objData.updateApi,
        delApi: objData.delApi,
        /**
         * 默认更新数据方法
         * @param {*} obj 
         */
        updateFunction: function (obj) {
            var value = obj.value
                , data = obj.data
                , field = obj.field
                , id = data[tableData.pk];
            var updateData = {};
            updateData[tableData.pk] = id;
            updateData[field] = value;
            $.post(tableData.updateApi, updateData, function (res) {
                res = JSON.parse(res);
                if (res.code) {
                    layer.msg('修改成功');
                } else {
                    layer.msg(res.msg);
                }
            });
        },
        /**
         * 默认编辑函数
         * @param {*} obj 
         */
        editFunction: function (obj) {
            var data = obj.data;
            SetPopup('编辑', capitalize(objData.model) + 'Setting');
            updateQueryStringParameter(objData.pk, data[objData.pk]);
        },
        /**
         * 默认查看函数
         * @param {*} obj 
         */
        detailFunction: function (obj) {
            var data = obj.data;
            SetPopup('查看', capitalize(objData.model) + 'Setting');
            updateQueryStringParameter(objData.pk, data[objData.pk]);
        },
        /**
         * 默认删除函数
         * @param {*} obj 
         */
        delFunction: function (obj) {
            var data = obj.data;
            layer.confirm('真的删除行么', function (index) {
                var id = data[tableData.pk];
                var delParam = {};
                delParam[tableData.pk] = id;
                $.post(tableData.delApi, delParam, function (res) {
                    res = JSON.parse(res);
                    if (res.code == 1) {
                        layer.msg('删除成功', { time: 1000, maxWidth: 160 });
                        obj.del();
                    } else {
                        layer.msg(res.msg);
                    }
                });
                layer.close(index);
            });

        },
    };
    /**如有声明函数，则替换默认 */
    if (objData.updateFunction != null && typeof objData.updateFunction == "function") {
        tableData.updateFunction = objData.updateFunction;
    }
    if (objData.delFunction != null && typeof objData.delFunction == "function") {
        tableData.delFunction = objData.delFunction;
    }
    if (objData.editFunction != null && typeof objData.editFunction == "function") {
        tableData.editFunction = objData.editFunction;
    }
    if (objData.detailFunction != null && typeof objData.detailFunction == "function") {
        tableData.detailFunction = objData.detailFunction;
    }
    /**生成table节点 */
    var tableHtml = '<table id="Table_All_' + tableData.model + '" class="layui-hide" lay-filter="' + tableData.model + '"></table>';
    $(tableData.elem).append(tableHtml);
    /**表格渲染 */
    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#Table_All_' + tableData.model
            , method: 'post'
            , url: tableData.tableApi
            , page: true
            , toolbar: 'default'
            , limit: 10
            , id : tableData.model
            , cols: [tableData.cols],
            parseData: function (res) { res.code = 0; return res; }
        });
        table.on('edit(' + tableData.model + ')', function (obj) { tableData.updateFunction(obj); });
        table.on('tool(' + tableData.model + ')', function (obj) { if (obj.event === 'del') { tableData.delFunction(obj); } else if (obj.event === 'edit') { tableData.editFunction(obj); } else if (obj.event === 'detail') { tableData.detailFunction(obj); } });
    });
}
/**
 * 表格重载
 * @param {*} t 表格
 * @param {*} api 接口地址
 * @param {*} where 接口额外参数
 */
function reloadTable(t, api, where = {}) {
    layui.use('table', function () { var table = layui.table; table.reload(t, { url: api, where: where }); });
}